/*
 * DictionaryRecord.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.dictionary.records;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Pattern;

import de.studiorutton.tre.AMatchResult;

public class LeftRightValuesRecord extends IRecord {
	private final static Pattern DOUBLE_COLLONS = Pattern.compile("::");
	private final static Pattern STROKES = Pattern.compile("[|]");
	private ArrayList<String> leftValues = new ArrayList<String>();
	private ArrayList<String> rightValues = new ArrayList<String>();
	
	private RecordRankingValues leftRank = new RecordRankingValues();
	private RecordRankingValues rightRank = new RecordRankingValues();
	
	public LeftRightValuesRecord() {
		
	}
	
	public RecordRankingValues getLeftRank() {
		return leftRank;
	}
	
	public RecordRankingValues getRightRank() {
		return rightRank;
	}
	
	
	public ArrayList<String> getLeftValues() {
		return leftValues;
	}
	
	public int getLeftValuesSize() {
		return leftValues.size();
	}

	public String getLeftValuesAsString() {
		return getValuesAsString(leftValues);
	}

	public String getLeftValuesAsStringFormatted() {
		return getValuesAsStringFormatted(leftValues);
	}

	public String getLeftNthValue(int nth) {
		return leftValues.get(nth);
	}

	public ArrayList<String> getRightValues() {
		return rightValues;
	}

	public int getRightValuesSize() {
		return rightValues.size();
	}

	public String getRightValuesAsString() {
		return getValuesAsString(rightValues);
	}

	public String getRightValuesAsStringFormatted() {
		return getValuesAsStringFormatted(rightValues);
	}

	public String getRightNthValue(int nth) {
		return rightValues.get(nth);
	}

	public String getAllValues() {
		return getValuesAsString(leftValues) + "::"
				+ getValuesAsString(rightValues);
	}
	
	public int getMinErrors() {
		return Math.min( leftRank.errors, rightRank.errors );
	}

	protected String getValuesAsString(ArrayList<String> values) {
		StringBuffer buf = new StringBuffer();
		Iterator<String> iter = values.iterator();
		if (iter.hasNext()) {
			buf.append(iter.next());
		}
		while (iter.hasNext()) {
			buf.append("|");
			buf.append(iter.next());
		}

		return buf.toString();
	}

	protected String getValuesAsStringFormatted(ArrayList<String> values) {
		StringBuffer buf = new StringBuffer();
		Iterator<String> iter = values.iterator();
		if (iter.hasNext()) {
			buf.append(iter.next());
		}
		while (iter.hasNext()) {
			buf.append("\n− ");
			buf.append(iter.next());
		}
		return buf.toString();
	}

	public static LeftRightValuesRecord parseDictionaryLine(String value)
			throws ParseException {

		String valuesTrimmed = value.trim();
		String values[] = DOUBLE_COLLONS.split(valuesTrimmed);

		LeftRightValuesRecord lrvr = new LeftRightValuesRecord();
		String leftValues[], rightValues[];
		if (values.length >= 1) {
			leftValues = STROKES.split(values[0]);
			for (String leftV : leftValues)
				lrvr.leftValues.add(leftV.trim());
		}
		if (values.length == 2) {
			rightValues = STROKES.split(values[1]);
			for (String rightV : rightValues)
				lrvr.rightValues.add(rightV.trim());
		}
		return lrvr;
	}

	public void calcRank(AMatchResult[] leftRightValues) {		
		int i = 0, size = 0, lastSize = 0;

		if (leftRightValues[0].position > -1) {
			for (i = 0; i < leftValues.size(); i++) {
				size += leftValues.get(i).length() + 1;
				if (size > leftRightValues[0].position) {
					leftRank.index = i;
					break;
				}
				lastSize = size;
			}
			leftRank.position = leftRightValues[0].position - lastSize;
			leftRank.end = leftRightValues[0].end - lastSize;
		}
		leftRank.cost = leftRightValues[0].cost;
		leftRank.errors = leftRightValues[0].errors;

		size = 0; lastSize = 0;
		if (leftRightValues[1].position > -1) {
			for (i = 0; i < rightValues.size(); i++) {
				size += rightValues.get(i).length() + 1;
				if (size > leftRightValues[1].position) {
					rightRank.index = i;
					break;
				}
				lastSize = size;
			}
			rightRank.position = leftRightValues[1].position - lastSize;
			rightRank.position = leftRightValues[1].end - lastSize;
		}
		rightRank.cost = leftRightValues[1].cost;
		rightRank.errors = leftRightValues[1].errors;
	}

}
